/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2019/6/1 0:35
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.aurora.core.init;

import com.hyacinth.aurora.core.exception.DestroyException;
import com.hyacinth.aurora.core.exception.InitializationError;
import com.hyacinth.aurora.core.exception.SingleClassInstanceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 初始器
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
public abstract class Initialization extends AbstractSingleton implements Init {

    protected final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    private final String instanceMame;

    protected Initialization() throws SingleClassInstanceException {
        super();
        instanceMame = this.getClass().getName();
    }

    @Override
    public void init() throws InitializationError {
        LOGGER.info("<<<<<<<<<<--[{}] - 初始化开始--->>>>>>>>", instanceMame);
        try {
            doInit();
        } catch (Throwable e) {
            LOGGER.info("<<<<<<<<<<--[{}] - 初始化出错--->>>>>>>>", instanceMame, e);
            throw new InitializationError(instanceMame + "初始化出错!!!", e);
        }
        LOGGER.info("<<<<<<<<<<--[{}] - 初始化结束--->>>>>>>>", instanceMame);
    }

    @Override
    public void destroy() throws DestroyException {
        LOGGER.info("<<<<<<<<<<--[{}] - destroy开始--->>>>>>>>", instanceMame);
        try {
            doDestroy();
        } catch (Throwable e) {
            LOGGER.info("<<<<<<<<<<--[{}] - destroy出错--->>>>>>>>", instanceMame, e);
            throw new DestroyException(instanceMame + "destroy出错!!!", e);
        }
        LOGGER.info("<<<<<<<<<<--[{}] - destroy结束--->>>>>>>>", instanceMame);
    }

    protected abstract void doInit() throws Exception;
    protected abstract void doDestroy() throws Exception;

}
